package com.toannv.handler;

import java.util.List;

import com.smartfoxserver.v2.core.ISFSEvent;
import com.smartfoxserver.v2.core.SFSEventParam;
import com.smartfoxserver.v2.entities.RoomSize;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.exceptions.SFSErrorCode;
import com.smartfoxserver.v2.exceptions.SFSErrorData;
import com.smartfoxserver.v2.exceptions.SFSException;
import com.smartfoxserver.v2.exceptions.SFSJoinRoomException;
import com.smartfoxserver.v2.extensions.BaseServerEventHandler;
import com.smartfoxserver.v2.game.SFSGame;
import com.toannv.definitions.ErrorId;
import com.toannv.definitions.KeyObjectId;
import com.toannv.definitions.ResponseId;
import com.toannv.helper.UserHelper;

public class JoinRoomHandler extends BaseServerEventHandler{

	@Override
	public void handleServerEvent(ISFSEvent event) throws SFSException {
		// TODO Auto-generated method stub
		
		final SFSGame roomEnter = (SFSGame) event.getParameter(SFSEventParam.ROOM);
		
		if(roomEnter.isGame()){
			final User sender = (User) event.getParameter(SFSEventParam.USER);
			
			RoomSize roomSize = roomEnter.getSize();
			int NumberUserInRoom = roomSize.getTotalUsers();
			
			if(NumberUserInRoom < roomEnter.getMaxUsers() && !roomEnter.isNotifyGameStarted()){
				
				new Thread(){
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						super.run();
						List<User> users = roomEnter.getUserList();
						
						try {
							roomEnter.addUser(sender);
						} catch (SFSJoinRoomException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
						SFSObject sfs_sender = UserHelper.getObjectUser(sender);
						send(ResponseId.USER_NEWER, sfs_sender, users);
					}
					
				}.start();
				
				List<User> players = roomEnter.getPlayersList();
				
				for(User player: players){
					SFSObject sfs_player = UserHelper.getObjectUser(player);
					send(ResponseId.USER_OLDER, sfs_player, sender);
				}
				
			}
			
			else if((NumberUserInRoom >= roomEnter.getMaxUsers() && (NumberUserInRoom < roomEnter.getMaxSpectators()+roomEnter.getMaxUsers()))
					|| roomEnter.isNotifyGameStarted()){
				new Thread(){
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						super.run();
						List<User> users = roomEnter.getUserList();
						
						try {
							roomEnter.addUser(sender);
						} catch (SFSJoinRoomException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						SFSObject sfs_sender = new SFSObject();
						sfs_sender.putUtfString(KeyObjectId.USER_NAME, sender.getName());
						send(ResponseId.SPECTATOR, sfs_sender, users);
					}
					
				}.start();
				
				List<User> players = roomEnter.getPlayersList();
				
				for(User player: players){
					SFSObject sfs_player = UserHelper.getObjectUser(player);
					send(ResponseId.USER_OLDER, sfs_player, sender);
				}
			}
			
			else if(roomEnter.isFull()){
				SFSErrorData err = new SFSErrorData(SFSErrorCode.JOIN_ROOM_FULL);
				err.addParameter(ErrorId.ROOM_FULL);
			}
		}
	}
}